﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;

namespace gpuimp.core.ColorConverter
{
    public class ColorConverter
    {      
        public static Color HSLToRGB(double h, double s, double l)
        {
            double r, g, b, v;

            r = g = b = l;

            if (l <= 0.5)
            {
                v = (l * (1.0 + s));
            }
            else
            {
                v = l + s - (l * s);
            }

            if (v > 0)
            {
                double m, sv, fract, vsf, mid1, mid2;
                int sextant;

                m = l + l - v;
                sv = (v - m) / v;
                h *= 6.0;
                sextant = (int)h;
                fract = h - sextant;
                vsf = v * sv * fract;
                mid1 = m + vsf;
                mid2 = v - vsf;
                switch (sextant)
                {
                    case 0:
                        r = v;
                        g = mid1;
                        b = m;
                        break;
                    case 1:
                        r = mid2;
                        g = v;
                        b = m;
                        break;
                    case 2:
                        r = m;
                        g = v;
                        b = mid1;
                        break;
                    case 3:
                        r = m;
                        g = mid2;
                        b = v;
                        break;
                    case 4:
                        r = mid1;
                        g = m;
                        b = v;
                        break;
                    case 5:
                        r = v;
                        g = m;
                        b = mid2;
                        break;
                }

            }
            Color cpc = new Color
            {
                R = Convert.ToByte(r * 255.0f),
                G = Convert.ToByte(g * 255.0f),
                B = Convert.ToByte(b * 255.0f)
            };            

            return cpc;
            
        }


    }
}
